package com.wanlian.util;

import java.lang.reflect.Method;

import com.wanlian.json.JsonStringUtil;

public class ReflectUtils {
    public static String createInvoke(Class<?> clazz, String methodName, Object param) {
        InvokeDescripsion descripsion = new InvokeDescripsion(clazz, methodName, param);
        String paramJson = JsonStringUtil.writeValueAsString(descripsion);
        return paramJson;
    }

    public static Object invoke(String clazzName, String methodName, Object... params) {
        try {
            Class<?> clazz = Class.forName(clazzName);
            Object obj = SpringContextUtil.getBean(clazzName);
            Method[] methods = clazz.getDeclaredMethods();
            Method callMethod = null;
            for (Method method : methods) {
                if (method.getName().equals(methodName)) {
                    callMethod = method;
                    break;
                }
            }
            callMethod.setAccessible(true);
            return (Object) callMethod.invoke(obj, params);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
